﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayerHelper;
using ObjectPersistence;
using Portal.API;
using KYO.DAL;

public partial class _Default : System.Web.UI.Page
{
    TabAssembler tabService = new TabAssembler();
    protected void Page_Load(object sender, EventArgs e)
    {
            int tabId = UrlRequestHandler.GetTabId();
            if (tabId < 0)
            {
               Response.Redirect("Default.aspx?tabId=3");
            }
            Tab tab = tabService.ReadSingleByKey(tabId);
            tabStyle = tab.CSS;
         
            PersistentList<ModuleIntall> moduleIntalls = tab.ModuleIntallColl;
            //Page page = (Page)HttpContext.Current.Handler;


            foreach (ModuleBase module in LoadModule(moduleIntalls, Position.BANNER))
            {
                PlaceBanner.Controls.Add(module);
                PlaceBanner.Controls.Add(new LiteralControl("<" + "br" + ">"));
            }

            foreach (ModuleBase module in LoadModule(moduleIntalls, Position.MAIN))
            {
                PlaceMain.Controls.Add(module);
                PlaceMain.Controls.Add(new LiteralControl("<" + "br" + ">"));
            }
            foreach (ModuleBase module in LoadModule(moduleIntalls, Position.RIGHT))
            {
                PlaceRight.Controls.Add(module);
                PlaceRight.Controls.Add(new LiteralControl("<" + "br" + ">"));
            }
            foreach (ModuleBase module in LoadModule(moduleIntalls, Position.MAINPROMO))
            {
                PlaceMainPromo.Controls.Add(module);
                PlaceMainPromo.Controls.Add(new LiteralControl("<" + "br" + ">"));
            }
            foreach (ModuleBase module in LoadModule(moduleIntalls, Position.MENU))
            {
               PlaceMenu.Controls.Add(module);
               PlaceMenu.Controls.Add(new LiteralControl("<" + "br" + ">"));
            }
            List<ModuleBase> list = LoadModule(moduleIntalls, Position.RIGHTPROMO);
            foreach (ModuleBase module in list)
            {
                PlaceRightPromo.Controls.Add(module);
                PlaceRightPromo.Controls.Add(new LiteralControl("<" + "br" + ">"));
            }
            
        
        

    }

    private List<ModuleBase> LoadModule(PersistentList<ModuleIntall> moduleIntalls, Position position)
    {
        List<ModuleBase> modules = new List<ModuleBase>();
        Page page = (Page)HttpContext.Current.Handler;
        foreach (ModuleIntall mo in moduleIntalls)
        {
            ModuleBase module = (ModuleBase)page.LoadControl("~/Modules/" + mo.ModuleObject.ModulePath + "/" + mo.ModuleObject.FileName);
            module.ModuleIntallId = mo.ID.ToString();
            module.InitModule("", "", mo.ModuleObject.ModulePath, "", true);
            module.GetDefaultConfig();

            Position modulePosition = (Position)module.Position;
            if (module.IsVisible)
            {
                if (modulePosition == position)
                {

                    modules.Add(module);
                }
            }
        }
        modules.Sort();

        return modules;

    }
    string tabStyle;
    public string LoadTabStyle
    {
        get { return
            @"<style type='text/css'>"+
            tabStyle+
        @"</style>";}
    }
   
}
